package galaxies;

import eventManager.EventManager;
import galaxies.enums.EGalaxyType;
import gameAppLayer.GameSettings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.vecmath.Vector3d;

import old.coordinate.CoordinateModel;

import model.universe.*;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Quad;
import com.jme3.texture.Texture;

import controller.AbstractController;
import controller.IController;


import stars.StarModel;
import stars.StarGenerator;
import utilities.*;
import view.EViewList;
import view.AbstractView;

/*
 * Factory for creating an elliptical galaxy
 * Input:  MUniverseSettings, index indicating where target galaxy is within MUniverseSettings
 * Output:  MGalaxy with Coordinates in an elliptical pattern, plus location vectors indicating
 * positions of MCoordinates to be filled in.
 */
public class GalaxyEllipticalBuilder {
	
	private static final Logger logger = Logger.getLogger(GalaxyEllipticalBuilder.class.getName());
	
	private HashMap<Integer, ArrayList<Integer>> galaxyTileLocations;
	
	public GalaxyEllipticalBuilder(){}
	
	//create cluster galaxy
	public ArrayList<IController> createGalaxy(
			GameSettings settings, 
			AssetManager assetManager, 
			String galRef, 
			ArrayList<ArrayList<Integer>> dims) {
		
		logger.log(Level.INFO, "FMEllipticalGalaxy: createModel()");
		GalaxyModel model = new GalaxyModel(settings.getGalSizes(galRef), settings.getGalaxyType(galRef), settings.getGalaxyDensity(galRef));
		GalaxyViewBuilder viewBuild = new GalaxyViewBuilder();
		HashMap<EViewList, AbstractView> view = viewBuild.buildViews(assetManager);
		GalaxyController controller = new GalaxyController();
		
		MVCMaker.linkElements(model, view, controller);
		
		ArrayList<IController> controllers = new ArrayList<IController>();
		controllers.add(controller);
		logger.log(Level.INFO, "GALAXY CREATED");
		
		model.setDims(dims);
		logger.log(Level.INFO, "LOAD LOCATIONS START");
		
		int largestDim = (int) findLargest(dims);
		
		galaxyTileLocations = HexLocationLoader.loadLocations(largestDim);
		
		logger.log(Level.INFO, "LOAD LOCATIONS FINISH");
		model.setLocations(galaxyTileLocations);
		logger.log(Level.INFO, "LOCATIONS ADDED TO GALAXY");
		
		return controllers;
	}
	
	public HashMap<Integer, ArrayList<Integer>> getLocations() {
		return this.galaxyTileLocations;
	}
	
	public double findLargest(ArrayList<ArrayList<Integer>> dims) {
		double dist;
		double maxDist = 0;		
		int x, y;
		for (ArrayList<Integer> point: dims) {
			x = point.get(0);
			y = point.get(1);
			
			dist = Math.sqrt(x*x + y*y);
			if (dist > maxDist) {
				maxDist = dist;				
			}			
		}
		
		return maxDist;
	}	
}